home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / database / packftp.zip / PACKFTP.C < prev    next >
C/C++ Source or Header  |  1994-12-22  |  6KB  |  289 lines

  1. /* Program:    packftp
  2.  * Author:    Mark R. Rinfret (mrr@mrsoft.network23.com)
  3.  * Date:    11/28/94
  4.  *
  5.  * Usage:    packftp <input_file> <output_file>
  6.  *
  7.  * Purpose:
  8.  *        This program converts the FTP Site List, currently maintained
  9.  *        by Perry Rovers (Perry.Rovers@kub.nl), into a file of records
  10.  *        containing quote-delimited, comma-separated values. The resulting
  11.  *        file is suitable for importing into a database of your choice,
  12.  *        though I created (and distributed with this program) a Microsoft
  13.  *        Access 2.0 database which works quite nicely.
  14.  *
  15.  *        This program is contributed to the public domain and the author
  16.  *        surrenders all rights to it. Please feel free to enhance it,
  17.  *        redistribute it, serve it with cream cheese, whatever. There's
  18.  *        certainly plenty of room for improvement. Note that there is
  19.  *        currently no error checking for values whose lengths exceed
  20.  *        VALUE_SIZE.
  21.  * 
  22.  * Instructions:
  23.  *        prepare the <input_file> by concatenating the various pieces of
  24.  *         the FTP Site List into one file. Remove all mail headers and
  25.  *        other 'noise'. A blank line between site definitions is OK.
  26.  *        No other alterations should be necessary and, other than the
  27.  *        Site entry (which must appear first), the order of the entries
  28.  *        could actually change without upsetting this program.
  29.  *
  30.  *        Next, run the packftp program. Example:
  31.  *
  32.  *            packftp ftp.raw ftp.imp
  33.  *
  34.  *        You can then import the resulting file into an appropriately
  35.  *        designed database.
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41.  
  42. #define BUFFER_SIZE        255
  43. #define VALUE_SIZE        4096
  44.  
  45. #define startFlag    "BEGIN_FTP_LIST"    /* Added by list preparer. */
  46.  
  47. #define FIELD_COUNT        13
  48.  
  49. static char        *titles[FIELD_COUNT] = {
  50.                     "Site",
  51.                     "Country",
  52.                     "GMT",
  53.                     "Date",
  54.                     "Source",
  55.                     "Alias",
  56.                     "Admin",
  57.                     "Organ",
  58.                     "Server",
  59.                     "System",
  60.                     "URL",
  61.                     "Comment",
  62.                     "Files",
  63.                     };
  64.  
  65. static char        *fields[FIELD_COUNT];
  66.  
  67. static int        maxLengths[FIELD_COUNT];
  68.  
  69. static char        buffer[BUFFER_SIZE];
  70.  
  71. FILE            *inFile = NULL;
  72. FILE            *outFile = NULL;
  73.  
  74. int                recordCount = 0;
  75. int                lineNumber = 0;
  76.  
  77. int
  78. FillBuffer(void)
  79. {
  80.     int        length = 0;
  81.  
  82.     while (! feof(inFile) && length == 0)
  83.     {
  84.         if (fgets(buffer, BUFFER_SIZE, inFile))
  85.         {
  86.             ++lineNumber;
  87.             length = strlen(buffer);
  88.             if (length)
  89.             {
  90.                 buffer[--length] = '\0'; /* Drop the newline */
  91.             }
  92.         }
  93.     }
  94.     return length;
  95. }
  96.  
  97.  
  98. void
  99. PutFields(int doTitles)
  100. {
  101.     int            i;
  102.     int            length;
  103.     char        *p;
  104.     char        *v;
  105.  
  106.     for (i = 0; i < FIELD_COUNT; ++i)
  107.     {
  108.         /* Replace all double quotes with single quotes. */
  109.  
  110.         v = (doTitles ? titles[i] : fields[i]);
  111.         if (! doTitles)
  112.         {
  113.             for (p = v; *p; ++p)
  114.                 if (*p == '"') *p = '\'';
  115.             length = strlen(v);                /* Gather statistics. */
  116.             if (length > maxLengths[i])
  117.                 maxLengths[i] = length;
  118.         }
  119.  
  120.         fprintf(outFile,"\"%s\",", v);
  121.  
  122.         if (! doTitles)
  123.             *v = '\0';                        /* Mark it used. */
  124.     }
  125.  
  126.     fprintf(outFile, "*EOR*\n");            /* Output end-of-record. */
  127. }
  128.  
  129. void
  130. ProcessKeywords(void)
  131. {
  132.     int        i;
  133.     char    *kw;
  134.     int        kwx = -1;
  135.     char    *value = NULL;
  136.  
  137.     PutFields(1);
  138.  
  139.     while (! feof(inFile) )
  140.     {
  141.         if (! *buffer)
  142.         {
  143.             FillBuffer();
  144.         }
  145.         /* See if column 7 contains a keyword. */
  146.         if (buffer[7] == ':')    
  147.             kw = strtok(buffer, " :");
  148.         else
  149.             kw = NULL;
  150.  
  151.         if (! kw )
  152.         {
  153.             /* Multi-line field value? */
  154.             if (value)
  155.             {
  156.                 /* Separate the lines of multi-line entries with a space. */
  157.                 if (*value) strcat(value, " ");
  158.                 strcat(value, buffer+9);
  159.             }
  160.             *buffer = '\0';
  161.         }
  162.         else
  163.         {
  164.             /* Recognize keyword? */
  165.             for (i = 0, kwx = -1; i < FIELD_COUNT; ++i)
  166.             {
  167.                 /* Do a case-insensitive compare. I found one instance
  168.                  * where this was necessary (SItes vs. Sites).
  169.                  */
  170.                 if (strcmpi(buffer, titles[i]) == 0)
  171.                 {
  172.                     kwx = i;
  173.                     break;
  174.                 }
  175.             }
  176.             if (kwx == -1)
  177.             {
  178.                 printf("*** Unrecognized keyword at line %d: '%s'\n",
  179.                         lineNumber, buffer);
  180.                 value = NULL;
  181.             }
  182.             else
  183.             {
  184.                 value = fields[kwx];
  185.             }
  186.  
  187.             *buffer = '\0';            /* Don't re-scan this buffer. */
  188.  
  189.             if (kwx == 0)            /* First field? */
  190.             {
  191.                 /* Flush the previous record? */
  192.                 if (recordCount)
  193.                 {
  194.                     PutFields(0);
  195.                 }
  196.                 ++recordCount;
  197.             }
  198.  
  199.             /* Save the value? */
  200.             if (value) strcat(value, buffer+9);
  201.         }
  202.     }    /* ! feof(inFile) */
  203.  
  204.     if (*fields[0]) 
  205.         PutFields(0);
  206. }
  207.  
  208. /* We enter ProcessList with startFlag in the buffer. */
  209. void
  210. ProcessList(void)
  211. {
  212.     int        i;
  213.  
  214.     /* This loop should only execute once since ProcessKeywords 
  215.      * doesn't give up control until the whole file has been read.
  216.      */
  217.     while (! feof(inFile))
  218.     {
  219.         if (FillBuffer())
  220.         {
  221.             if (buffer[7] == ':') ProcessKeywords();    
  222.         }
  223.     }
  224.  
  225.     puts("\nMaximum field lengths:");
  226.     for (i = 0; i < FIELD_COUNT; ++i)
  227.     {
  228.         printf("\t%-12s: %d\n", titles[i], maxLengths[i]);
  229.     }
  230. }
  231.  
  232. int
  233. main(int argc, char **argv)
  234. {
  235.     int        gotStart = 0;
  236.     int        i;
  237.  
  238.     if (argc != 3)
  239.     {
  240.         puts("usage: pack_ftp_list <infile> <outfile>");
  241.         exit(1);
  242.     }
  243.  
  244.     for (i = 0; i < FIELD_COUNT; ++i)
  245.     {
  246.         if (! (fields[i] = calloc(1,VALUE_SIZE)) )
  247.         {
  248.             puts("*** Out of memory! ***");
  249.             exit(1);
  250.         }
  251.     }
  252.  
  253.     inFile = fopen(argv[1], "r");
  254.     if (! inFile)
  255.     {
  256.         perror(argv[1]);
  257.         exit(1);
  258.     }
  259.     outFile = fopen(argv[2], "w");
  260.     if (! outFile)
  261.     {
  262.         perror(argv[2]);
  263.         exit(1);
  264.     }
  265.  
  266.     while (! feof(inFile) )
  267.     {
  268.         if (FillBuffer())
  269.         {
  270.             if (strcmp(buffer, startFlag) == 0)
  271.             {
  272.                 gotStart = 1;
  273.                 ProcessList();
  274.             }
  275.         }
  276.     }
  277.  
  278.     if (! gotStart)
  279.     {
  280.         printf("*** Never found '%s' string.\n", startFlag);
  281.         exit(1);
  282.     }
  283.  
  284.     if (inFile) fclose(inFile);
  285.     if (outFile) fclose(outFile);
  286.  
  287.     return 0;
  288. }
  289.